package com.ruoyi.project.manhua.page.controller;

import java.util.List;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.qiniu.QiNiuYunUtils;
import com.ruoyi.project.manhua.webLook.service.MhpictureService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.manhua.page.domain.MhPage;
import com.ruoyi.project.manhua.page.service.IMhPageService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 漫画篇章页数Controller
 *
 * @author yuan
 * @date 2021-01-19
 */
@Controller
@RequestMapping("/manhua/page")
public class MhPageController extends BaseController {
    private String prefix = "manhua/page";

    @Autowired
    private IMhPageService mhPageService;

    @Autowired
    private QiNiuYunUtils qiNiuYunUtils;

    @Autowired
    private MhpictureService mhpictureService;

    @RequiresPermissions("manhua:page:view")
    @GetMapping("/{id}/{nameId}")
    public String page(@PathVariable("id") Long id, @PathVariable("nameId") Long nameId, ModelMap map) {
        map.put("chapterId", id);
        map.put("nameId", nameId);

        return prefix + "/page";
    }


    /**
     * @param nameId
     * @param chapterId
     * @param map
     * @return
     */
    @GetMapping("/look/{nameId}/{chapterId}")
    public String look(@PathVariable("nameId") Long nameId, @PathVariable("chapterId") Long chapterId, ModelMap map) {
        List<String> list = mhpictureService.pageDataService(nameId, chapterId);

        map.put("listImg", list);
        return prefix + "/look";
    }

    /**
     * 查询漫画篇章页数列表
     */
    @RequiresPermissions("manhua:page:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(MhPage mhPage) {
        startPage();
        List<MhPage> list = mhPageService.selectMhPageList(mhPage);
        TableDataInfo dataTable = getDataTable(list);
        /*转换图片 链接*/
        list.forEach((e) -> {
            if (StringUtils.isNotEmpty(e.getSrc())) {
                if (e.getSrc().indexOf("http") == -1) {
                    e.setSrc(qiNiuYunUtils.getUrlImg(e.getSrc()));
                }
            }
        });

        return dataTable;
    }

    /**
     * 导出漫画篇章页数列表
     */
    @RequiresPermissions("manhua:page:export")
    @Log(title = "漫画篇章页数", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(MhPage mhPage) {
        List<MhPage> list = mhPageService.selectMhPageList(mhPage);
        ExcelUtil<MhPage> util = new ExcelUtil<MhPage>(MhPage.class);
        return util.exportExcel(list, "page");
    }

    /**
     * 新增漫画篇章页数
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存漫画篇章页数
     */
    @RequiresPermissions("manhua:page:add")
    @Log(title = "漫画篇章页数", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(MhPage mhPage) {
        return toAjax(mhPageService.insertMhPage(mhPage));
    }

    /**
     * 修改漫画篇章页数
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        MhPage mhPage = mhPageService.selectMhPageById(id);
        mmap.put("mhPage", mhPage);
        return prefix + "/edit";
    }

    /**
     * 修改保存漫画篇章页数
     */
    @RequiresPermissions("manhua:page:edit")
    @Log(title = "漫画篇章页数", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(MhPage mhPage) {
        return toAjax(mhPageService.updateMhPage(mhPage));
    }

    /**
     * 删除漫画篇章页数
     */
    @RequiresPermissions("manhua:page:remove")
    @Log(title = "漫画篇章页数", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(mhPageService.deleteMhPageByIds(ids));
    }
}
